<html lang="en">
	<head>
		<title>Yuka | Navmesh</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" type="text/css" href="../../lib/styles.css">
		<link rel="shortcut icon" type="image/x-icon" href="https://mugen87.github.io/yuka/favicon.ico">
	</head>
<body>

	<section id="info">
		<p>
			Click on the navigation mesh to define a new target for the game entity.<br/>
			The colored areas represent the convex regions of the navigation mesh.
		</p>
	</section>

	<script type="module">

		import * as YUKA from '../../../build/yuka.module.js';
		import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.109/build/three.module.js';
		import * as DAT from 'https://cdn.jsdelivr.net/npm/dat.gui@0.7.7/build/dat.gui.module.js';

		import { createGraphHelper } from '../../graph/common/js/GraphHelper.js';
		import { createConvexRegionHelper } from '../common/js/NavMeshHelper.js';

		let renderer, scene, camera, navMeshGroup;

		let raycaster, mouseCoordinates;

		let entityManager, time, vehicle, navMesh;

		let pathMaterial, pathHelper, graphHelper;

		const params = {
			showNavigationGraph: true
		};

		init();

		function init() {

			scene = new THREE.Scene();

			camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 1000 );
			camera.position.set( 25, 25, 0 );
			camera.lookAt( scene.position );

			//

			raycaster = new THREE.Raycaster();
			mouseCoordinates = new THREE.Vector2();

			//

			pathMaterial = new THREE.LineBasicMaterial( { color: 0xff0000 } );
			pathHelper = new THREE.Line( new THREE.BufferGeometry(), pathMaterial );
			pathHelper.visible = false;
			scene.add( pathHelper );

			//

			const vehicleGeometry = new THREE.ConeBufferGeometry( 0.25, 1, 16 );
			vehicleGeometry.rotateX( Math.PI * 0.5 );
			vehicleGeometry.translate( 0, 0.25, 0 );
			const vehicleMaterial = new THREE.MeshNormalMaterial();

			const vehicleMesh = new THREE.Mesh( vehicleGeometry, vehicleMaterial );
			vehicleMesh.matrixAutoUpdate = false;
			scene.add( vehicleMesh );

			// renderer

			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );

			//

			window.addEventListener( 'resize', onWindowResize, false );
			document.addEventListener( 'mousedown', onMouseDown, false );

			// dat.gui

			const gui = new DAT.GUI( { width: 300 } );

			gui.add( params, 'showNavigationGraph', 1, 30 ).onChange( ( value ) => {

				if ( graphHelper ) graphHelper.visible = value;

			} );

			gui.open();

			// load navigation mesh

			const loader = new YUKA.NavMeshLoader();

			loader.load( '../common/navmeshes/basic/navmesh.gltf' ).then( ( navigationMesh ) => {

				// visualize convex regions

				navMesh = navigationMesh;
				navMeshGroup = createConvexRegionHelper( navMesh );

				scene.add( navMeshGroup );

				// visualize graph

				const graph = navMesh.graph;

				graphHelper = createGraphHelper( graph, 0.2 );
				scene.add( graphHelper );

				// game setup

				entityManager = new YUKA.EntityManager();
				time = new YUKA.Time();

				vehicle = new YUKA.Vehicle();
				vehicle.maxSpeed = 1.5;
				vehicle.maxForce = 10;
				vehicle.setRenderComponent( vehicleMesh, sync );

				const followPathBehavior = new YUKA.FollowPathBehavior();
				followPathBehavior.active = false;
				vehicle.steering.add( followPathBehavior );

				entityManager.add( vehicle );

				animate();

			} );

		}

		function onWindowResize() {

			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();

			renderer.setSize( window.innerWidth, window.innerHeight );

		}

		function onMouseDown( event ) {

			mouseCoordinates.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
			mouseCoordinates.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;

			raycaster.setFromCamera( mouseCoordinates, camera );

			const intersects = raycaster.intersectObject( navMeshGroup, true );

			if ( intersects.length > 0 ) {

				findPathTo( new YUKA.Vector3().copy( intersects[ 0 ].point ) );

			}

		}

		function findPathTo( target ) {

			const from = vehicle.position;
			const to = target;

			const path = navMesh.findPath( from, to );

			pathHelper.visible = true;
			pathHelper.geometry.dispose();
			pathHelper.geometry = new THREE.BufferGeometry().setFromPoints( path );

			const followPathBehavior = vehicle.steering.behaviors[ 0 ];
			followPathBehavior.active = true;
			followPathBehavior.path.clear();

			for ( const point of path ) {

				followPathBehavior.path.add( point );

			}

		}

		function animate() {

			requestAnimationFrame( animate );

			const delta = time.update().getDelta();

			entityManager.update( delta );

			renderer.render( scene, camera );

		}

		function sync( entity, renderComponent ) {

			renderComponent.matrix.copy( entity.worldMatrix );

		}

	</script>

</body>
</html>
